package com.epoint.androidphone.mobileoa.service.mqtt;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.epoint.androidmobile.bizlogic.mail.task.Mail_GetList_Task;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.service.receiver.MessageReceiver;
import com.epoint.androidphone.mobileoa.ui.mail.MailBoxListView;
import com.ibm.mqtt.MqttAdvancedCallback;

/**
 * @author liyc
 * @time 2013-8-15 上午8:58:44
 * @annotation MQTT回调
 */
public class EpointMQTTCallback implements MqttAdvancedCallback {

	private Context con;
	
	public EpointMQTTCallback(Context con)
	{
		this.con = con;
	}

	@Override
	public void publishArrived(String paramString, byte[] paramArrayOfByte,
			int paramInt, boolean paramBoolean) throws Exception {
		System.out.println("MQTT publishArrived");

		String msg = "新邮件："+new String(paramArrayOfByte);
		Intent intent = new Intent(MessageReceiver.Name);
		intent.putExtra("msg", msg);
		con.sendBroadcast(intent);


		NotificationManager nm = (NotificationManager)con.getSystemService(Context.NOTIFICATION_SERVICE);
		CharSequence from = con.getString(R.string.app_name);
		Intent im = new Intent(con, MailBoxListView.class);
		im.putExtra("boxType", Mail_GetList_Task.INBOX_TYPE);
		im.putExtra("viewtitle", "收件箱");
		PendingIntent contentIntent = PendingIntent.getActivity(con, 0,
				im, 0);
		Notification notif = new Notification(R.drawable.mailnotopen, msg,
				System.currentTimeMillis());
		notif.defaults |= Notification.DEFAULT_ALL;
		notif.setLatestEventInfo(con, from, msg, contentIntent);
		nm.notify(1, notif);
	}

	@Override
	public void connectionLost() throws Exception {
		System.out.print("MQTT connectionLost");
	}

	@Override
	public void unsubscribed(int paramInt) {
		System.out.println("MQTT unsubscribed");
	}

	@Override
	public void subscribed(int paramInt, byte[] paramArrayOfByte) {
		System.out.println("MQTT subscribed");
	}

	@Override
	public void published(int paramInt) {
		System.out.println("MQTT published");
	}

}
